
package control.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.math.CrimeResult;
import bean.CharacterBEAN;
import bean.CrimeResultBEAN;
import dao.ActionDAO;
import dao.CharacterDAO;

/**
 * Servlet implementation class Crime
 */
public class Crime extends HttpServlet {
	private static final long	serialVersionUID	= 1L;
	private CharacterDAO		characterDAO;
	private ActionDAO			actionDAO;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Crime() {
		super();
	}

	public void init( ServletConfig config ) throws ServletException {
		characterDAO = (CharacterDAO) config.getServletContext().getAttribute(
				"CharacterDAO" );
		actionDAO = (ActionDAO) config.getServletContext().getAttribute(
				"ActionDAO" );
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		HttpSession session = request.getSession();
		int accountID = (Integer) session.getAttribute( "ID" );
		CharacterBEAN characterBEAN = characterDAO.getCharacterByID( accountID );
		if( action.equals( "simpleCrime" ) ) {
			CrimeResult crime = new CrimeResult( actionDAO, characterDAO );
			CrimeResultBEAN crs = crime.doSimpleCrime( ( Integer
					.parseInt( request.getParameter( "simpleCrime" ) ) ),
					characterBEAN );
			request.setAttribute( "character", characterBEAN );
			if( crs.isWorked() ) {
				request.setAttribute( "winningPrice", crs.getPrice() );
				goTo( "WEB-INF/logged/crime/crimeResultWin.jsp", request,
						response );
				return;
			} else if( crs.isJail() ) {
				goTo( "WEB-INF/logged/crime/crimeResultJail.jsp", request,
						response );
				return;
			} else {
				goTo( "WEB-INF/logged/crime/crimeResultLose.jsp", request,
						response );
				return;
			}
		}
		if( action.equals( "bigCrime" ) ) {
			CrimeResult crime = new CrimeResult( actionDAO, characterDAO );
			CrimeResultBEAN crs = crime.doBigCrime( ( Integer.parseInt( request
					.getParameter( "bigCrime" ) ) ), characterBEAN );
			request.setAttribute( "character", characterBEAN );
			if( crs.isWorked() ) {
				request.setAttribute( "winningPrice", crs.getPrice() );
				goTo( "WEB-INF/logged/crime/crimeResultWin.jsp", request,
						response );
				return;
			} else if( crs.isJail() ) {
				goTo( "WEB-INF/logged/crime/crimeResultJail.jsp", request,
						response );
				return;
			} else {
				goTo( "WEB-INF/logged/crime/crimeResultLose.jsp", request,
						response );
				return;
			}
		}
		if( action.equals( "shootArea" ) ) {
			CrimeResult crime = new CrimeResult( actionDAO, characterDAO );
			CrimeResultBEAN crs = crime.doShootArea( characterBEAN );
			request.setAttribute( "character", characterBEAN );
			if( crs.isWorked() ) {
				goTo( "WEB-INF/logged/crime/shootSkillWin.jsp", request,
						response );
				return;
			} else {
				goTo( "WEB-INF/logged/crime/shootSkillFail.jsp", request,
						response );
				return;
			}
		}
		if( action.equals( "murder" ) ) {
			// TODO handel murder af
		}
	}

	private void goTo( String url, HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher( url );
		dispatcher.forward( request, response );
	}
}
